package com.leadzone.utils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Timestamp;

public class UDPClient {
	
	static DatagramSocket client;
	private static String _addr;
	private static int _port;
	/**
	 *  ADDR 127.0.0.1
	 *  PORT 5123
	 * */
	public UDPClient(String addr, int port) {
			_addr=addr;
			_port=port;
	 
	}

	public boolean getCurrentPic(String puid)   {
		
		try {
			client = new DatagramSocket();
			Timestamp d = new Timestamp(System.currentTimeMillis());
			String sendStr = puid+"+PICTURE"+"+"+d.getTime()+"@";
			byte[] sendBuf;
			sendBuf = sendStr.getBytes();
			
			InetAddress addr = InetAddress.getByName(_addr);
			DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, addr, _port);
			client.send(sendPacket);		
			
			System.out.println("发送成功:" + sendStr);
			
		/*		byte[] recvBuf = new byte[100];
			DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
			
		client.receive(recvPacket);
			
			String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength());

			System.out.println("收到:" + recvStr);*/
			
			client.close();
			return true;
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public boolean getVedio(String puid)   {
		
		try {
			client = new DatagramSocket();
			Timestamp d = new Timestamp(System.currentTimeMillis());
			String sendStr = puid+"+VEDIO"+"+"+d.getTime()+"@";
			byte[] sendBuf;
			sendBuf = sendStr.getBytes();
			
			InetAddress addr = InetAddress.getByName(_addr);
			DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, addr, _port);
			client.send(sendPacket);		
			
			System.out.println("发送成功:" + sendStr);
			
		/*		byte[] recvBuf = new byte[100];
			DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
			
		client.receive(recvPacket);
			
			String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength());

			System.out.println("收到:" + recvStr);*/
			
			client.close();
			return true;
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}